/**
 * 2015-12-21
 * TeacherDaoImpl.java
 * author:sherlochao
 */
package com.sherlochao.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.sherlochao.dao.TeacherDao;
import com.sherlochao.model.Course;
import com.sherlochao.model.Student;
import com.sherlochao.model.Teacher;

/**
 * @author yinchao
 *
 */
@Repository("teacherDao")
public class TeacherDaoImpl implements TeacherDao{

	@Resource
	private SessionFactory sessionFactory;
	
	@Resource
	private TeacherDao teacherDao;
	
	public Session getSession() {
		return sessionFactory.openSession();
	}
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public TeacherDao getTeacherDao() {
		return teacherDao;
	}

	public void setTeacherDao(TeacherDao teacherDao) {
		this.teacherDao = teacherDao;
	}

	@Override
	public Teacher get(String teacherId) {
		// TODO Auto-generated method stub
		String hql = "from Teacher where id = ?";
		Query query = this.getSession().createQuery(hql);
		query.setString(0, teacherId);
		return (Teacher) query.uniqueResult();
	}

	@Override
	public String save(Teacher entity) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delete(String id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Teacher load(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	//查询该老师所带的所有课程
	@Override
	public List<Course> listCourseByTeacherId(String teacherId) {
		// TODO Auto-generated method stub
		String hql = "from Course where teacher_id = ?";
		Query query = this.getSession().createQuery(hql);
		query.setString(0, teacherId);
		List<Course> courses = query.list();
		return courses;
	}

	//查询该老师所带课程的学生数量
	@Override
	public List<Integer> listStudentByTeacherId(String teacherId) {
		// TODO Auto-generated method stub
		List<Course> courses = teacherDao.listCourseByTeacherId(teacherId);
		List<Integer> students = new ArrayList<Integer>();
		
		for(int i = 0; i < courses.size(); i++){
			String hql = "from StudyCourse where course_id = ?";
			Query query = this.getSession().createQuery(hql);
			query.setString(0,courses.get(i).getCourseNumber());
			students.add(query.list().size());
		}
		return students;
	}

}
